<?
	/* Se asegura que el archivo es incluido por el index */
		defined( 'LOAD_MODULE_ACTION' ) or die( 'No esta permitido el acceso directo a ' . __FILE__ );
	/* Cuando se hace el include del archivo automaticamente crea un objeto y lo guarda en una variable temporal */
		$this->oTmpObject = new Tarea_lst( $this );

	class Tarea_lst extends Lst
	{
	function Tarea_lst( &$oModule )
		{
			$this->Lst ( $oModule );
		}
	function setLevel()
		{
			switch ( $this->getOptionView() )
			{
				case 'E':
				case 'C':
					$this->aShowColumns = array( 'Tarea', 'Vto');
					break;
				case 'T':
				case 'L':
					$this->aShowColumns = array( 'bEstrella', 'ncRotulo', 'Pr','Tarea' );
					break;
				case 'R':
					//$this->aShowColumns = array( 'bEstrella', 'ncRotulo', 'Alta', 'Vto', 'Completo', 'Prioridad', 'Tarea', 'Contenido', 'Categoria', 'Usuario','Responsable');
					break;
			}
			//Si viene la persona no muestra el responsable
			//if ( array_key_exists( 'ncPersona' , $this->aFields )  && array_search( 'Responsable',$this->aShowColumns) ) unset($this->aShowColumns[array_search( 'Responsable',$this->aShowColumns)]);
		}
	function initialize()
		{
			$this->oFiltro = $this->oModule->loadClass( 'Tarea_frm' );
			$this->aFields = array_merge ( (array)$this->aFields,  (array)$this->oFiltro->getFieldArray() );
			
			$this->aFields['ncProyecto'] = getParam('ncProyecto');
			if ( getParam('bEmpezadas') ) $this->aFields['bEmpezadas'] = getParam('bEmpezadas');
			if ( getParam('bRealizadas') ) $this->aFields['bRealizadas'] = getParam('bRealizadas');
			if ( getParam('bPendientes') ) $this->aFields['bPendientes'] = getParam('bPendientes');
			if ( getParam('ncCategoriaTarea') ) $this->aFields['ncCategoriaTarea'] = getParam('ncCategoriaTarea');
			$this->aFields['ncGrupo'] = getParam('ncGrupo') ?  getParam('ncGrupo'):  obtenerMiGrupo();
			if ( !$this->aFields['ncPersona']  ) $this->aFields['ncPersona'] = getParam('Res_ncPersona') ? getParam('Res_ncPersona'): getParam('ncPersona');
			$this->aFields["bSinSubtarea"] = getParam("bSinSubtarea", 1);
			if ( $this->aFields['ncCategoriaTarea'] < 0  )
			{
				$this->aFields['bPendientes'] = ( $this->aFields['ncCategoriaTarea'] == -1  ) ? 1: 0 ;
				$this->aFields['bRealizadas'] = ( $this->aFields['ncCategoriaTarea'] == -2  ) ? 1: 0 ;
				$this->aFields['bEmpezadas'] = ( $this->aFields['ncCategoriaTarea'] == -3  ) ? 1: 0 ;
				$this->aFields['ncCategoriaTarea']  = '';
			}

			$this->setTitle ( "Tareas" );
			if ( $this->oModule->getTarget() == 'body_main' )
			{
				$oTab =& $this->setIndex( INDEX_USER, TAB_TOP , 'ncCategoriaTarea');
				$cSql = "SELECT ncCategoriaTarea, cdCategoriaTarea FROM atCategoriaTarea
							WHERE ncGrupo = " .  $this->aFields['ncGrupo'];
				$oTab->setMatrix( $this->oDatabase->getMatrix($cSql)  );
				$oTab->addTab(  'Pendientes', -1 );
				$oTab->addTab(  'Empezadas', -3 );
				$oTab->addTab(  'Realizadas', -2 );
				$oTab =& $this->setIndex( INDEX_USER, TAB_BOTTOM , 'ncPersona', false);
				if ( $this->aFields['ncProyecto'] )
					$cSql  = "SELECT Per.ncPersona, cApellido FROM atPersona AS Per JOIN atProyectoPersona PPe ON PPe.ncPersona = Per.ncPersona WHERE ncProyecto = " . $this->aFields['ncProyecto'];
				else
				{
					$cSqlTareas = "SELECT DISTINCT ncPersona FROM atTareaResponsable AS TRe JOIN atTarea as Tar ON  Tar.ncTarea = TRe.ncTarea WHERE dCompleto is NULL AND ncGrupo = " . $this->aFields['ncGrupo'];
					$cSql = "SELECT ncPersona, cApellido FROM atPersona WHERE ncPersona IN ( $cSqlTareas ) ";
				}
				$oTab->setMatrix( $this->oDatabase->getMatrix($cSql)  );
			}

			$this->setSummary( "Esta tabla contiene las Tareas" );
			$this->cField = "ncTarea[]";
			$this->oModule->setBack( $this->cUrlBase );
			$this->oFecha = new DateObject();
			$this->oHoy = new DateObject();
			$this->oHoy->setToday();

			$this->addColumnStar ( 'bEstrella', MODIFICAR_ESTRELLA_TAREA, 'ncTarea', 'bEstrella' );
			$this->addColumnTag( 'ncRotulo', ACTUALIZAR_TAG_TAREA, 'ncTarea', 'atTareaRotulo', $this->oDatabase->getKeyArray('SELECT ncRotulo, cdRotulo FROM atRotuloTarea WHERE ncGrupo = ' . obtenerMiGrupo() ) );

			$this->addColumnDate ( 'Alta', 'dDesde' );
			$this->addColumnDate ( 'Vto', 'dVencimiento' );
			$this->addColumnDate ( 'Realizada', 'dCompleto' );
			$this->addColumnDate ( 'Empezo', 'dInicio' );			
			$this->addColumnImageArray( 'Pr', 'ncPrioridad', true, '', array( 1 => 'prioridad_1.gif', 2 => 'prioridad_2.gif', 3 => 'prioridad_3.gif' ) );
			$this->addColumn ( 'Prioridad', 'cdPrioridad' );
			$this->addColumn ( 'Proyecto', 'cdProyecto' );
			$this->addColumn ( 'Tarea', 'cdTarea' );
			$this->addColumn ( 'Contenido', 'cDescripcion' );
			$this->addColumn ( 'Responsable', '' );
			$this->addColumn ( 'Categoria', 'Categoria' );
			$this->addColumn ( 'Usuario', 'cUsuario' );
			$this->setColumnAction ( 'Tarea', VER_TAREA, 'ncTarea');
			$this->setColumnAction ( 'Proyecto', VER_PROYECTO, 'ncProyecto');

			//$cSql = "SELECT ncPersona, cApellido FROM atPersona WHERE ncPersona IN ( SELECT ncPersona FROM atPersonal )  ";
			//$this->addDimension( 'Res.ncPersona', 'Responsable' , $this->oDatabase->getKeyArray($cSql) );
			$cSql = "SELECT ncPrioridad, cdPrioridad FROM acPrioridad";
			$this->addDimension( 'Tar.ncPrioridad', 'Prioridad',  $this->oDatabase->getKeyArray($cSql) );
			$cSql = "SELECT ncCategoriaTarea, cdCategoriaTarea FROM atCategoriaTarea";
			$this->addDimension( 'Tar.ncCategoriaTarea', 'Categoria',  $this->oDatabase->getKeyArray($cSql) );
			$aTipo = array( '1' => "Pendiente", '2' => "Realizada", '3' => "Empezada");
			$this->addDimension( 'CASE WHEN dCompleto is NULL THEN CASE WHEN dInicio is NULL THEN 1 ELSE 3 END  ELSE 2 END', 'Tipo' , $aTipo  );
			$this->addMetric( 'count(*)', 'Registros' );

		}
	function makeColumn ( $aRow, $cName )
		{
			switch( $cName )
			{
			case 'KeyDay':
				$this->oFecha->setValue( $aRow['dVencimiento'] );
				$cReturn =  $this->oFecha->getValue();
				break;
			case 'KeyLookUp':
				$cReturn = $aRow['ncTarea'];
				break;
			case 'Pr':
				if ( $aRow['ncPrioridad'] )	$cReturn = "<img alt='prioridad" . $aRow['ncPrioridad'] . "' src='" . $this->oModule->cUrlImages . "/prioridad_" . $aRow['ncPrioridad'] .".gif'/>";
				break;
			case 'Responsable':
				$cReturn = implode( "," , $this->oDatabase->getArray( "SELECT " . $this->oDatabase->dbConcat( "cNombre", "' '",  "cApellido" ) . " FROM  atTareaResponsable as Res  JOIN atPersona as Per ON Res.ncPersona = Per.ncPersona WHERE ncTarea = " . $aRow['ncTarea'] ) );
				break;
			default:
				return false;
			break;
			}
			return $cReturn ;
		}
	function makeRowProperties ( $aRow )
		{
			if ( empty($aRow['dCompleto'])   && $aRow['dVencimiento'] && $this->oHoy->dateDiff( "d",  $aRow['dVencimiento'] ) <= 0   )  return " style='color: red;' ";
		}
	function query()
		{
			extract( $this->aFields );
			$ncMiGrupo = obtenerMiGrupo();
			// Arma el Query
				$cSql = "SELECT Tar.bEstrella, Tar.dDesde, Tar.dVencimiento, Tar.dCompleto, Tar.dInicio, Tar.ncCategoriaTarea, Tar.ncTarea, Tar.ncPrioridad, Tar.cdTarea, cdPrioridad, cdProyecto, CTa.cdCategoriaTarea,  Tar.cDescripcion,  " . $this->oDatabase->dbConcat( "Usu.cNombre", "' '",  "Usu.cApellido" ) . " as cUsuario
										FROM atTarea as Tar
									LEFT JOIN atCategoriaTarea as CTa
										ON CTa.ncCategoriaTarea = Tar.ncCategoriaTarea
									LEFT JOIN atProyecto as Pro
										ON Pro.ncProyecto = Tar.ncProyecto
									LEFT JOIN acPrioridad as Pri
										ON Pri.ncPrioridad = Tar.ncPrioridad
									LEFT JOIN atPersona as Usu
										ON Usu.ncPersona = Tar.ncUsuario ";
			// Arma el Filtro de la Consulta
				$cSql .= armarJoin (" JOIN atTareaResponsable as TRe ON TRe.ncTarea = Tar.ncTarea AND TRe.ncPersona = $ncPersona " , $ncPersona );
				$this->oWhere->addArray ( $this->oFiltro->getFilterdArray(), 'Tar' );
				if ( !$ncTareaPadre ) $this->oWhere->add ( "Tar.ncTarea", $ncTarea );
				else $this->oWhere->add ( "Tar.ncTareaPadre", $ncTareaPadre);				
				if ( !$ncTarea ) $this->oWhere->add( 'Tar.ncGrupo' , $ncGrupo );
				if ( $ncMiGrupo )
					$this->oWhere->addExpression ( "Tar.ncVisibilidad", " (Tar.ncVisibilidad <> " . VISIBILIDAD_PRIVADO . " OR Tar.ncGrupo = $ncMiGrupo) " );
				else
					$this->oWhere->addExpression ( "Tar.ncVisibilidad", " Tar.ncVisibilidad = " . VISIBILIDAD_PUBLICO );
				$this->oWhere->addNull( "dCompleto", $bPendientes );
				$this->oWhere->addNotNull( "dInicio", $bEmpezadas );
				$this->oWhere->addNotNull( "dCompleto", $bRealizadas );
				$this->oWhere->addNull( "ncTareaPadre", $bSinSubtarea == 1 && !$ncTareaPadre);
				return $cSql . $this->oWhere->make();
		}
	function makeListActions()
		{
			$ncGrupo = getParam( 'ncGrupo');
			//$cParametroRegistro =  $this->getFieldString();
			if ( $ncGrupo  == '' || $ncGrupo  == obtenerMiGrupo() )
			{
				$cParametroRegistro =  "ncProyecto=" . $this->aFields["ncProyecto"] ;
				if ( $this->aFields["ncTareaPadre"] ) $cParametroRegistro .=  "&amp;ncTareaPadre=" . $this->aFields["ncTarea"] ;
				if ( $this->aFields["ncPersona"] ) $cParametroRegistro .=  "&amp;ncPersona=" . $this->aFields["ncPersona"] ;
				$this->addListAction ( AGREGAR_TAREA, $cParametroRegistro, 'I', VENTANA_SECUNDARIA );
			}
		}
	function makeRowActions( $aRow )
		{
			 $cParametroRegistro =  "ncTarea=" . $aRow["ncTarea"];
			$cParametroRegistroB =  "ncTarea[]=" . $aRow["ncTarea"];
			if ( empty($aRow['dCompleto'])  )
			{
				if ( empty($aRow['dInicio'])  )
				{
					$this->addRowAction ( REALIZAR_TAREA, $cParametroRegistroB);
					$this->addRowAction ( EMPEZAR_TAREA, $cParametroRegistroB);
				} else
				{
					$this->addRowAction ( AGREGAR_TAREA_REGISTRO, $cParametroRegistroB, '', DIV_POPUP);
					$this->addRowAction ( FINALIZAR_TAREA, $cParametroRegistroB);
				}
			} else
			{
				$this->addRowAction ( DESREALIZAR_TAREA, $cParametroRegistro );
			}
			if ( $this->getOptionsRowAction() == 'L' )
			{
				$this->addRowAction ( ELIMINAR_TAREA, $cParametroRegistroB );
			} else
			{
				$this->addRowAction ( AGREGAR_TAREA, "ncTareaPadre=" . $aRow["ncTarea"] );
				$this->addRowAction ( MODIFICAR_TAREA, $cParametroRegistro, '', VENTANA_SECUNDARIA );
				$this->addRowAction ( ELIMINAR_TAREA, $cParametroRegistroB );
			}
		}
	function makeSelectionActions()
		{
			$this->addSelectionAction ( REALIZAR_TAREA );
			$this->addSelectionAction ( ELIMINAR_TAREA );
		}
	function makeComplex()
		{
			// SubTareas
			$oLst = $this->oModule->loadClass ( "tarea_registro_lst" );
			$oLst->aFields['ncTarea'] = $this->aFields['ncTarea'];
			$oLst->setTitle('Registro');
			$oLst->setNoPaging();
			$oLst->open();
			if ( $oLst->oRecordset->count() > 0 )
			{
				$oLst->makeTab();
			}
		}
	}
?>